package com.szh.htmltopdf.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.RuntimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping
@RestController
@Slf4j
@SuppressWarnings("unused")
public class TestController {

    /**
     * linux 安装目录。注意linux安装之后，环境变量要配。（如果环境变量不配，写绝对路径）
     */
    private static final String BASH_CMD = "wkhtmltopdf";
    /** rpm 安装的默认路径（绝对路径）*/
    private static final String LINUX_BASH_CMD = "/usr/local/bin/wkhtmltopdf";

    private static final String HTML = " /root/szh/tes/index.html";
    private static final String PDF = " /root/szh/tes/test.pdf";
    private static final String AGRS=" --margin-top 18mm --margin-bottom 8mm --footer-spacing 5 --header-spacing 5";
    private static final String[] CMD_ONE_ARG = { BASH_CMD+AGRS+HTML+PDF};




    @GetMapping("/hu")
    public String getPdf(){
        if (FileUtil.isWindows()){
            String cmd=getWinWkPath()+AGRS+HTML+PDF;
            String s = RuntimeUtil.execForStr(cmd);
            
            return s;
        }else {
            String s = RuntimeUtil.execForStr(CMD_ONE_ARG);
            
            return s;
        }
    }
    
  
    public static void main(String[] args) {
        if (FileUtil.isWindows()){
            String cmd=getWinWkPath() +AGRS+HTML+PDF;
            String s = RuntimeUtil.execForStr(cmd);
            
        }else {
            String s = RuntimeUtil.execForStr(CMD_ONE_ARG);
            
        }
    }

    public static String getWinWkPath() {
        return ResourceUtil.getResource("pdf/wkhtmltopdf.exe").getPath();
    }







}
